package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.AdSwitchConfig;
import com.lebang.domain.TaskRouse;
import com.lebang.example.AdSwitchConfigExample;
import com.lebang.service.NewRouseService;
import com.lebang.service.sys.AdSwitchConfigService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


/**
 * ad_switch_config
 *
 * @author hwq
 * @date 2020-12-30
 */
@Controller
@RequestMapping("/sys/adswitchconfig")
public class AdSwitchConfigController {
    @Autowired
    private AdSwitchConfigService baseService;
    @Autowired
    private NewRouseService newRouseService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/sys/adswitchconfig/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model, HttpSession session) {

        AdSwitchConfig vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AdSwitchConfig();
            vo.setSdkKey("CSJ");
        }
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("list", rouses);
        model.addAttribute("sdk",rouses);
        model.addAttribute("item", vo);
        return "/sys/adswitchconfig/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(AdSwitchConfig vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        baseService.init();
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        baseService.init();
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        AdSwitchConfigExample example = new AdSwitchConfigExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        AdSwitchConfigExample.Criteria criteria = example.createCriteria();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
}
